# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import fields, osv, orm
import logging
import addons

class res_company(osv.osv):
    _name = "res.company"
    _description = '组织架构'
    _order = 'name'
    
    def name_get(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        if not ids:
            return []
        reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
        res = []
        for record in reads:
            name = record['name']
            if record['parent_id']:
                name = record['parent_id'][1]+' / '+name
            res.append((record['id'], name))
        return res

    def _dept_name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
        res = self.name_get(cr, uid, ids, context=context)
        return dict(res)
        
    _columns = {
        'complete_name': fields.function(_dept_name_get_fnc, type="char", string='全称'),
    }        
class village_organization(orm.Model):
    _name = "village.organization"
    _description = "组织架构"
    
    def name_get(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        if not ids:
            return []
        reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
        res = []
        for record in reads:
            name = record['name']
            if record['parent_id']:
                name = record['parent_id'][1]+' / '+name
            res.append((record['id'], name))
        return res

    def _dept_name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
        res = self.name_get(cr, uid, ids, context=context)
        return dict(res)
    
    _columns = {
        'name': fields.char('组织架构名称', size=64, required=True),
        'complete_name': fields.function(_dept_name_get_fnc, type="char", string='名称'),
        'parent_id': fields.many2one('village.organization', '上级单位', select=True),
        'child_ids': fields.one2many('village.organization', 'parent_id', '下属单位'),
        'note': fields.text('Note'),
    }

class village_resident_move(osv.osv):
    """户籍迁移"""
    _name = "village.resident.move"
    _description = "户籍迁移"
    _columns = {
        'resident': fields.many2one('village.resident', '迁移人', required = True),
        'direction': fields.selection([('迁入', '迁入'), ('迁出', '迁出')], '迁入/出', required = True),
        'reason': fields.char('迁移原因', size=50, required = True),
        'move_date': fields.date('迁移时间', required = True),
        'move_location': fields.char('由什么地方迁入/迁出到什么地方', required = True)
    }

class village_address(osv.osv):
    """户籍地址"""
    
    _name = "village.address"
    _description = "户籍地址"
    
    def name_get(self, cr, uid, ids, context=None):
        if not ids:
            return []
        reads = self.browse(cr, uid, ids, context=context)
        res = []
        for record in reads:
            name = record.name
            if record.organization:
                name = record.organization.complete_name + name
            res.append((record.id, name))
        return res

    def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
        res = self.name_get(cr, uid, ids, context=context)
        return dict(res)
        
    _columns = {
        'organization': fields.many2one('village.organization', '所属单位', required = True),
        'complete_name': fields.function(_dept_name_get_fnc, type="char", string='名称'),        
        'name': fields.char('门牌号', size = 50, required = True)
    }
    
class village_resident(osv.osv):
    """户籍"""
    _name = "village.resident"
    _description = "户籍"
    _inherits = {'resource.resource': "resource_id"}
    
    def _find_move_in(self, cr, uid, ids, name, args, context=None):
        res = {}
        for resident in self.browse(cr, uid, ids, context=context):
            move_objs = self.get_pool("village.resident.move").browse(cr, uid, resident.move_history_ids, context=context)
            moves = []
            for move in move_objs:
                
                if move.direction == '迁入':
                    moves.append('%s，因 %s由%s迁入' % (move.move_date, move.reason, move.move_location))
                else:
                    moves.append('%s，因 %s迁出到%s' % (move.move_date, move.reason, move.move_location))
            
            if moves:
                res[resident.id] = "/r/n".join(moves)  
        return res
        
    _columns = {
        'name': fields.char('姓名', size=10, required=True),
        'organization': fields.many2one('village.organization', '所在部门'),
        'is_householder': fields.boolean('是否是户主'),
        'householder': fields.many2one('village.resident', '户主'),
        'relationship': fields.selection([('丈夫', '丈夫'), ('妻子', '妻子')], '与户主关系'),
        #'householder_name': fields.many2one('village.people', '户主'),
        'gender': fields.selection([('男', '男'),('女', '女')], '性别'),
        'identification_id': fields.char('身份证号', size=18, required=True),
        'photo': fields.binary('照片'),
        'team': fields.char('组别', size=10, required=True, help='格式：01'),        
        'birthday': fields.char('出生日期', size=10, help='格式如：1980-01-01'),
        'householder_address': fields.char('户口所在地', size=20, help='地址不用输入镇村信息'),
        #'full_householder_address': fields.char('户口所在地', size=20, help='地址不用输入镇村信息'),
        'address_home': fields.char('目前住址', size=50),
        'phone': fields.char('联系电话', size=32, readonly=False),
        'mobile_phone': fields.char('手机', size=32, readonly=False),
        'residence_type': fields.selection([('常住', '常住'), ('寄宿', '寄宿')], '居住类型'),
        'education_level': fields.selection([('博士','博士'), ('硕士', '硕士'), ('初中',' 初中')], '文化程度'),
        'nation': fields.selection([('汉族','汉族'), ('布依族', '布依族'), ('白族',' 白族')], '民族'),
        'political_status': fields.selection([('清白','清白'), ('中共党员', '中共党员'), ('团员',' 团员')], '政治面貌'),
        'native_place': fields.char('籍贯', size=50),
        'marital': fields.selection([('未婚', '未婚'), ('已婚', '已婚'), ('离异', '离异'), ('丧偶', '丧偶'), ('再婚', '再婚')], '婚姻状况'),
        'work_location': fields.char('工作单位', size=32),
        'is_basic_living': fields.boolean('是否低保'),
        'is_village_treatment': fields.boolean('是否享受村待遇'),
        'has_citizen_insurance': fields.boolean('是否城保'),
        'has_village_insurance': fields.boolean('是否农村养老保险'),
        'is_physical_disability': fields.boolean('是否残疾'),
        'physical_disability_type': fields.selection([('视力残疾', '视力残疾'), ('听力残疾', '听力残疾'), ('言语残疾', '言语残疾'), ('智力残疾', '智力残疾'), ('肢体残疾', '肢体残疾')], '残疾类别'),
        'notes': fields.text('备注'),
        'move_history_ids': fields.one2many('village.resident.move', 'resident', readonly=True),
        'move_history': fields.function(_find_move_history, string='迁入/迁出历史', readonly=True ),
        'status': fields.selection([('在册','在册'), ('已迁出', '已迁出'), ('去世',' 去世')], '状态', readonly=True),
    }
    
    def _get_photo(self, cr, uid, context=None):
        photo_path = addons.get_module_resource('hr','images','photo.png')
        return open(photo_path, 'rb').read().encode('base64')

    _defaults = {
        'photo': _get_photo,
        'marital': '未婚',
    }
    
class res_users(osv.osv):
    _inherit = 'res.users'
    _description = 'User'

    _columns = {
        'context_organization_id': fields.many2one('village.organization', '单位'),
    }    

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
